﻿using System.Collections.Generic;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.ShopInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.ShopInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.ShopInfo
{
    public class ShopSupportsInfoReponsitory : SqlServerRespository<ShopSupportInfoEntity>,
        IShopSupportsInfoReponsitory
    {
        public ShopSupportsInfoReponsitory()
        {
            Context = new WechatMallContext();
        }

        public IEnumerable<ShopSupportInfoEntity> GetShopSupportsInfoById(long shopid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<ShopSupportInfoEntity>();
            var sql = $@"SELECT {querysql} FROM shopsupportsinfo 
            WHERE shopid=@shopid";
            var result = GetList(sql, new {shopid});
            return result;
        }

        public IEnumerable<ShopSupportInfoEntity> GetShopSupportsInfoByAppId(string appId)
        {
            var querysql = DapperHelper.CreateColSqlByMap<ShopSupportInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM shopsupportsinfo a
            INNER JOIN appshoprelationship b ON a.shopid=b.shopid
            WHERE b.appid=@appid";
            var result = GetList(sql, new {appId});
            return result;
        }

        public void AddInfo(ShopSupportInfoEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void RemoveInfo(ShopSupportInfoEntity entity, UnitOfWork unit = null)
        {
            Remove(entity, unit);
        }

        public void RemoveInfoByShopid(long shopid, UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from shopsupportsinfo where shopid=@shopid", Context, unit, new {shopid});
        }

        public void EditInfo(ShopSupportInfoEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }
    }
}